#include "stdio.h"
#include "math.h"

int getWei(int num)
{
    if(num==0)
    {
        return 1;
    }
    double tmp=log10(num);
    int r=(int)(tmp)+1;
    return r;
}

int has(int num,int key,int ppp)
{
    if(num<key)
    {
        return 0;
    }
    if(num==key)
    {
        return 1;
    }
    
    int tmp=num;

    if(key==0)
    {
        do{
            if(tmp%ppp==0)
            {
                return 1;
            }
            tmp=tmp/10;
        }while(tmp>ppp);
    }
    else{
        do{
        if((tmp-key)%ppp==0)
        {
            return 1;
        }
        tmp=tmp/10;
    }while(tmp>=key);
    }
    
    return 0;
}

int compare(int  a1,int  a2){
    if(a1>a2)
    {
        return 1;
    }
    return 0;
}

void sort(int* array,int n)
{
    int i,j;
    int** queue=new int*[n];
    int start=0;
    int end=1;
    queue[start]=new int[2];
    queue[start][0]=0;
    queue[start][1]=n;
    //开始快排
    while (start<end)
    {
        int s=queue[start][0];
        int e=queue[start][1];
        
        i=s;
        j=e-1;
        int tmpIndex=j;
        int tmp=array[tmpIndex];
        while (i<j)
        {
            if(tmpIndex==j)
            {
                //前面大于后面
                if(compare(array[i],tmp))
                {
                    array[tmpIndex]=array[i];
                    tmpIndex=i;
                    // tmp=array[tmpIndex];
                }
            }
            else
            {
                //前面大于后面
                if(compare(tmp,array[j]))
                {
                    array[tmpIndex]=array[j];
                    tmpIndex=j;
                    // tmp=array[tmpIndex];
                }
            }

            if(tmpIndex==j)
            {
                i++;
            }
            else{
                j--;
            }

            
        }

        array[i]=tmp;

        start++;
        if(s<i)
        {
            end++;
            queue[end-1]=new int[2];
            queue[end-1][0]=s;
            queue[end-1][1]=i;
        }

        if(i+1<e)
        {
            end++;
            queue[end-1]=new int[2];
            queue[end-1][0]=i+1;
            queue[end-1][1]=e;
        }
    }

    for(i=0;i<end;i++)
    {
        delete queue[i];
    }
    delete queue;
}


int main(){
    int* iii;
    int* rrr;
    int ilen;
    int rlen=0;
    int i,j;
    scanf("%d",&ilen);
    iii=new int[ilen];
    for(i=0;i<ilen;i++)
    {
        scanf("%d",&iii[i]);
    }
    int tmp_len;
    scanf("%d",&tmp_len);
    int* tmp=new int[tmp_len];
    for(i=0;i<tmp_len;i++)
    {
        scanf("%d",&tmp[i]);
    }
    sort(tmp,tmp_len);

    rrr=new int[tmp_len];
    rrr[rlen]=tmp[0];
    rlen++;
    for(i=1;i<tmp_len;i++)
    {
        if(tmp[i]!=rrr[rlen-1])
        {
            rrr[rlen]=tmp[i];
            rlen++;
        }
    }
    delete tmp;

      
    int* ppp=new int[rlen];
    for(i=0;i<rlen;i++)
    {
        int wei=getWei(rrr[i]);
        ppp[i]=pow(10,wei);
    }

    int** result=new int*[rlen];
    int* num=new int[rlen];
    int total=0;
    for(i=0;i<rlen;i++)
    {
        result[i]=new int[ilen];
        num[i]=0;
        for(j=0;j<ilen;j++)
        {
            if(has(iii[j],rrr[i],ppp[i])){
                result[i][num[i]]=j;
                num[i]++;
            }
        }
        if(num[i]>0)
        {
            total+=num[i]*2+2;
        }
    }

    printf("%d",total);
    for(i=0;i<rlen;i++)
    {
        if(num[i]>0)
        {
            printf(" %d %d",rrr[i],num[i]);
            for(j=0;j<num[i];j++)
            {
                printf(" %d %d",result[i][j],iii[result[i][j]]);
            }
        }
    }

    for(i=0;i<rlen;i++)
    {
        delete result[i];
    }
    delete result;
    delete ppp;
    delete rrr;
    delete iii;
    delete num;
    
    return 0;
}